﻿using System;
using System.Windows.Forms;
using cashbook_psql_cs.BLL;

namespace cashbook_psql_cs
{
    public partial class FrmAdd : Form
    {
        //将mainForm实例通过构造函数方式传过来，而非重新new一个实例
        private readonly FrmMain _mainForm;
        private readonly RelationBillBLL _billBll = new RelationBillBLL();
        //通过公共属性传递
        public int PassedId { get; set; } // 公共属性
        public FrmAdd(FrmMain mainForm)
        {
            InitializeComponent();
            _mainForm = mainForm;
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            //手动计算屏幕分辨率居中
            Screen screen = Screen.PrimaryScreen;
            this.Left = (screen.WorkingArea.Width - this.Width) / 2;
            this.Top = (screen.WorkingArea.Height - this.Height) / 2;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            DateTime sztime ;
            sztime = DateTimePicker2.Value.Date;
            string sztype;
            decimal amount;
            string cate;
            string account;
            string relperson;
            string remark;
            sztype = ComboBox4.Text;
            amount = decimal.Parse(TextBox1.Text);
            cate = TextBox5.Text;
            account = ComboBox2.Text;
            relperson = ComboBox3.Text;
            remark = TextBox4.Text;
            RelationBill rb = new RelationBill
            {
                SZType = sztype,
                Amount = amount,
                SZTime = sztime,
                Cate = cate,
                Account = account,
                RelPerson = relperson,
                Remark = remark
            };
            try {
                FrmMain frmMain = new FrmMain();
                RelationBillBLL relationBillBLL = new RelationBillBLL();
                relationBillBLL.AddRelationBill(rb);
                MessageBox.Show("添加成功！");
                frmMain.DataGridView1.DataSource = relationBillBLL.FillDatasByRelationBill();
            }
            catch (Exception ex){
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
      
            

        }

        private void Button2_Click(object sender, EventArgs e)
        {            
            RelationBill rb = new RelationBill
            {
                SZType = ComboBox4.Text,
                Amount = decimal.Parse(TextBox1.Text),
                SZTime = DateTimePicker2.Value.Date,
                Cate = TextBox5.Text,
                Account = ComboBox2.Text,
                RelPerson = ComboBox3.Text,
                Remark = TextBox4.Text
            };
            int id = this.PassedId;
            rb.Id= id;
            try
            {

                // RelationBillBLL relationBillBLL = new RelationBillBLL();
                _billBll.ModifyRelationBill(rb);
                MessageBox.Show("更新成功！");
                _mainForm.DataGridView1.DataSource = _billBll.FillDatasByRelationBill();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
